package io.github.meta.ease.websocket.tyrus.client;

import jakarta.websocket.ClientEndpoint;
import jakarta.websocket.CloseReason;
import jakarta.websocket.ContainerProvider;
import jakarta.websocket.OnClose;
import jakarta.websocket.OnMessage;
import jakarta.websocket.OnOpen;
import jakarta.websocket.Session;
import jakarta.websocket.WebSocketContainer;

import java.net.URI;
  
@ClientEndpoint
public class WebSocketClient {  
  
    @OnOpen
    public void onOpen(Session session) {
        System.out.println("Connected to server");  
        try {  
            session.getBasicRemote().sendText("Hello Server");  
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
    }  
  
    @OnMessage
    public void onMessage(String message) {  
        System.out.println("Received message from server: " + message);  
    }  
  
    @OnClose
    public void onClose(Session session, CloseReason closeReason) {
        System.out.println("Session " + session.getId() + " closed: " + closeReason);  
    }  
  
    public static void main(String[] args) {  
        WebSocketContainer container = ContainerProvider.getWebSocketContainer();
        String uri = "ws://localhost:8025/websocket";
        System.out.println("Connecting to " + uri);  
        try {  
            container.connectToServer(WebSocketClient.class, URI.create(uri));  
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
    }  
}